# Create your views here.
from django.http import HttpResponse
from django.http import Http404

from models import User
from models import Word
from models import Comment
#import simplejson as json
from django.core import serializers
from django.template import loader, Context
from datetime import datetime
import string

def get_words(request):
    words = Word.get_top10_list()
    json_serializer = serializers.get_serializer("json")()
    json_serializer.serialize(words, ensure_ascii=False)
    data = json_serializer.getvalue()
    return HttpResponse(data)

def get_word(request, pk):
    word = Word.objects.get(pk=pk)
    t = loader.get_template('fc/word.html')
    c = Context({'word': word})
    return HttpResponse(t.render(c)) 

def vote(request,wpk,nsb):
    if request.method != 'POST':
        return HttpResponse('Not Found')
    if nsb != 'nb' and nsb != 'sb' and nsb != 'unknown':
        return HttpResponse('WNSB')
    
    #tmp
    try:
        user = User.objects.get(pk = 1)
    except:
        return HttpResponse("Not Found")
    #
    try:
        word = Word.objects.get(pk = wpk)
    except:
        return HttpResponse("Not Found")
    comment_nb = 0
    comment_sb = 0
    shared = 0
    if nsb == 'nb':
        comment_nb = 1
        word.click_nb()
    elif nsb == 'sb':
        comment_sb = 1
        word.click_sb()
    elif nsb == 'unknown':
        shared = 1
        word.click_not_known()
    content = request.body()
    ctype = -1
    if comment_nb == 1 and comment_sb != 1:
        ctype = 1
    elif comment_nb != 1 and comment_sb == 1:
        ctype = 0
    else:
        ctype = -1
    #insert(user,word,nsb text,time)
    #c = Comment(user, word, comment_nb, comment_sb, shared, content, type, time)
    c = Comment(user=user, word=word, comment_nb=comment_nb, comment_sb=comment_sb, shared=shared, content=content, type=ctype,created_at=datetime.now())
    c.save()
    return HttpResponse('OK')

def load_comment(request,wpk):
    if request.method == 'GET':
        dict = request.GET
    else:
        return HttpResponse("Not Found")
    try:
        word = Word.objects.get(pk = wpk)
    except:
        return HttpResponse("Not Found")
    try:
        start = string.atoi(dict.__getitem__('start'))
        count = string.atoi(dict.__getitem__('count'))
    except KeyError:
        start = 0
        count = 10
    if start < 0:
        start = 0
    size = Comment.comment_sum(word)
    if count >= size:
        count = size
    try:
        comments = Comment.load_comments(word,start,count)
    except:
        return HttpResponse("Wrong Para")
    json_serializer = serializers.get_serializer("json")()
    json_serializer.serialize(comments, ensure_ascii=False)
    data = json_serializer.getvalue()
    return HttpResponse(data)
    
def user(request, id):
    t = loader.get_template('fc/user.html')
    c = Context()
    return HttpResponse(t.render(c)) 

def user_new(request):
    t = loader.get_template('fc/user.html')
    c = Context()
    return HttpResponse(t.render(c)) 
    
def user_login(request):

    t = loader.get_template('fc/user.html')
    c = Context()
    return HttpResponse(t.render(c))     